﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;

namespace TIS.SPAuditDemo
{
    public class Utils
    {
        /// <summary>
        /// Returns a datatable with the audit values for all items in the site collection.
        /// Include version Histroy  or no version history.
        /// </summary>        
        /// <param name="site"></param>
        /// <param name="history"></param>
        /// <returns></returns>
        public DataTable getSiteCustomAuditTable(SPSite site, bool history)
        {

            DataTable dt = new DataTable();
            SPAuditQuery wssQuery = new SPAuditQuery(site);
            SPAuditEntryCollection auditCol = site.Audit.GetEntries(wssQuery);
            SPCustomAuditEntryCollection spcent = new SPCustomAuditEntryCollection(site);
            foreach (SPAuditEntry spa in auditCol)
            {
                spcent.Add(new SPCustomAuditEntry(spa));
            }

            return spcent.toDataTable();
        }

        /// <summary>
        /// Returns a datatable with the audit values for all items in the site collection.
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public DataTable getListCustomAuditFilteredTable(SPSite site, SPList list)
        {
            SPWeb rootWeb = site.RootWeb;
            DataTable dt = new DataTable();
            SPAuditQuery wssQuery = new SPAuditQuery(site);
            wssQuery.RestrictToList(list);
            SPAuditEntryCollection auditCol = site.Audit.GetEntries(wssQuery);
            SPCustomAuditEntryCollection spcent = new SPCustomAuditEntryCollection(site);
            foreach (SPAuditEntry spa in auditCol)
            {
                spcent.Add(new SPCustomAuditEntry(spa));
            }

            return spcent.toDataTable();
        }

        /// <summary>
        /// Returns a datatable with the audit values .
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public DataTable getItemCustomAuditFilteredTable(SPSite site, SPListItem item)
        {
            SPWeb rootWeb = site.RootWeb;
            DataTable dt = new DataTable();
            SPAuditQuery wssQuery = new SPAuditQuery(site);
            wssQuery.RestrictToListItem(item);
            SPAuditEntryCollection auditCol = site.Audit.GetEntries(wssQuery);
            SPCustomAuditEntryCollection spcent = new SPCustomAuditEntryCollection(site);
            foreach (SPAuditEntry spa in auditCol)
            {
                spcent.Add(new SPCustomAuditEntry(spa));
            }
            return spcent.toDataTable();
        }

        /// <summary>
        /// Returns a datatable with the audit values for a single web.
        /// </summary>
        /// <param name="site"></param>
        /// <returns></returns>
        public DataTable getWebCustomAuditFilteredTable(SPSite site, SPWeb web)
        {
            DataTable dt = new DataTable();
            SPAuditEntryCollection auditCol = web.Audit.GetEntries();
            SPCustomAuditEntryCollection spcent = new SPCustomAuditEntryCollection(site);
            foreach (SPAuditEntry spa in auditCol)
            {
                spcent.Add(new SPCustomAuditEntry(spa));
            }
            return spcent.toDataTable();
        }




    }
}
